# -*- coding:utf-8 -*-
__doc__ = '审批详情页操作'

from selenium.webdriver.common.by import By

from PageClass.basePage import BasePage
from Util import logger, ZTCCLOUDError


class EcsApprovalDetailPage(BasePage):
    # 挂起、加签、转交操作入口
    _dropdown = (By.XPATH, '//div[@class="el-dropdown"]')

    def move_toDropDown(self):
        self.click(*self._dropdown)
        self.moveToElement(*self._dropdown)

    # 单据审批页面审批意见
    _approvalComment = (By.XPATH, '//div[@class="bill-area approval"]//textarea[@class="el-textarea__inner"]')

    def input_approvalComment(self, text):
        self.send_text(text, *self._approvalComment)

    # ---------------------- 加签操作 ----------------------

    # 加签
    _addSign = (By.XPATH, '//li[@id="addSign"]')

    def click_addSignButton(self):
        self.click(*self._addSign)

    # 加签位置
    _signLocationInput = (By.ID, 'form_addSignPosition')

    def click_signLocation(self):
        self.click(*self._signLocationInput)

    # 加签位置内容选择框
    _signLocationSelect = (By.XPATH, "/html/body[@class='el-popup-parent--hidden']/"
                                     "div[@class='el-select-dropdown el-popper']/"
                                     "div[@class='el-scrollbar']/"
                                     "div[contains(@class, 'el-select-dropdown__wrap el-scrollbar__wrap')]/"
                                     "ul[@class='el-scrollbar__view el-select-dropdown__list']/li")

    def get_signLocationContent(self):
        return self.find_elements(*self._signLocationSelect)

    def select_signLocation(self, signLocation):
        elementList = self.get_signLocationContent()
        flag = False
        for element in elementList:
            if element.text == signLocation:
                self._clickElement(element)
                flag = True
        if not flag:
            raise ZTCCLOUDError(f"未找到到名称为：{signLocation} 的加签位置，请确认")

    # 加签人按钮位置
    _signPerson = (By.XPATH, '//div[@title="加签人"]//button')

    def click_signPerson(self):
        element = self.find_elements(*self._signPerson)
        self._clickElement(element[-1])

    # 审批意见
    _approvalOptions = (By.ID, 'form_opinion')

    def input_options(self, text):
        self.send_text(text, *self._approvalOptions)

    def click_submitButton(self):
        self.click(*(By.XPATH, '//*[@id="form"]/div[4]/div/button[2]'))

    # ---------------------- 转交操作 ----------------------

    # 转交
    _transfer = (By.ID, 'transfer')

    def click_transferButton(self):
        self.click(*self._transfer)

    # 转交人按钮位置
    _transferLocationButton = (By.ID, 'form_loginName')

    def click_transferLocationButton(self):
        self.click(*self._transferLocationButton)

    # 选择转交人
    def selectTransferPerson(self, **kwargs):
        self.selectValueFromSmallTable(**kwargs)

    # 提交按钮位置
    _transferSubmit = (By.XPATH, '//div[@aria-label="选择人员"]/div[@class="el-dialog__body"]//button[2]')

    def click_transferSubmit(self):
        self.click(*self._transferSubmit)

    # ---------------------- 挂起/解挂操作 ----------------------

    # 挂起
    _hangUp = (By.ID, 'hangUp')

    def click_hangUpButton(self):
        self.click(*self._hangUp)

    # 确定按钮
    _hangUpConfirm = (By.XPATH, '//div[@class="el-message-box"]//button[2]')

    def click_hangUpConfirm(self):
        self.click(*self._hangUpConfirm)

    # 解挂
    _hangOut = (By.ID, 'hangOut')

    def click_hangOutButton(self):
        self.click(*self._hangUp)

    def click_hangOutConfirm(self):
        self.click_hangUpConfirm()
